package com.talkweb.securitycamera.slice;

import com.talkweb.historylib.component.HistoryListComponent;
import com.talkweb.player.components.PlayerFraction;
import com.talkweb.securitycamera.ResourceTable;
import com.talkweb.securitycamera.common.base.BaseFractionAbilitySlice;
import com.talkweb.securitycamera.common.constans.Constant;
import com.talkweb.securitycamera.common.constans.FileType;
import com.talkweb.securitycamera.common.constans.ScreenChangeType;
import com.talkweb.securitycamera.common.interfaces.dataprocess.FileCallback;
import com.talkweb.securitycamera.common.interfaces.dataprocess.IFileProcessor;
import com.talkweb.securitycamera.common.interfaces.history.ICameraProcessor;
import com.talkweb.securitycamera.common.interfaces.history.IHistory;
import com.talkweb.securitycamera.common.interfaces.play.IPlayCallBack;
import com.talkweb.securitycamera.data.hadoopDB.HadoopDbProvider;
import com.talkweb.securitycamera.data.hadoopFile.SimpleImageProcessor;
import com.talkweb.securitycamera.data.hadoopFile.SimpleVideoProcessor;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.IEventListener;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.MsgConstant;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.PhotoEvent;
import com.talkweb.securitycamera.utils.eventlib.communicationevent.PhotoEventManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;

public class MainAbilitySlice extends BaseFractionAbilitySlice implements ICameraProcessor, FileCallback, IEventListener<PhotoEvent> {

    private PlayerFraction mPlayFraction;

    private IHistory mHistoryLayout;

    private HadoopDbProvider hadoopDbProvider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        PhotoEventManager.getInstance().registerEvent(this, MsgConstant.EventUserTopic.COMMON_EVENT_PIC_DATA_CHANGR);
    }

    @Override
    protected void onStop() {
        super.onStop();
        PhotoEventManager.getInstance().unRegisterEvent(this, MsgConstant.EventUserTopic.COMMON_EVENT_PIC_DATA_CHANGR);
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_main;
    }

    @Override
    protected void initView(Intent intent) {
        super.initView(intent);
        mPlayFraction = new PlayerFraction();
        addFraction(ResourceTable.Id_play_layout, mPlayFraction);
        mPlayFraction.setPlayCallback(new IPlayCallBack() {
            @Override
            public void exitPlayCallBack() {
                onBackPressed();
            }

            @Override
            public void onScreenChange(ScreenChangeType type) {
                if (mPlayFraction != null) {
                    DirectionalLayout.LayoutConfig layoutConfig =
                            (DirectionalLayout.LayoutConfig) mPlayFraction.getComponent().getLayoutConfig();
                    layoutConfig.height = (type == ScreenChangeType.SCREEN_CHANGE_ENTER_FULL) ?
                            DirectionalLayout.LayoutConfig.MATCH_PARENT : mPlayFraction.getOriginHeight();
                    mPlayFraction.getComponent().setLayoutConfig(layoutConfig);
                }
            }
        });

        DirectionalLayout directionalLayout = findComponent(ResourceTable.Id_history_layout);
        mHistoryLayout = new HistoryListComponent(this, this);
        directionalLayout.addComponent(mHistoryLayout.getHistoryFraction());
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    @Override
    public IFileProcessor getCameraProcessor(FileType type) {
        if (hadoopDbProvider == null) {
            hadoopDbProvider = new HadoopDbProvider(this, this);
        }
        return hadoopDbProvider;
    }

    @Override
    public void onDataChanged(FileType type) {
        mHistoryLayout.refreshFraction(type);
    }


    @Override
    public void onEvent(PhotoEvent event) {
        if (event != null && event.getTopic().equals(MsgConstant.EventUserTopic.COMMON_EVENT_PIC_DATA_CHANGR)) {
            boolean hasChange = (boolean) event.getMsgObj();
            if (hasChange) {
                mHistoryLayout.refreshFraction(FileType.FILE_TYPE_IMAGE);
            }
        }
    }
}
